package cn.amaake.magicplugin.satoken.autoconfigure;

import cn.amaake.magicplugin.satoken.Interceptor.CustomAuthorizationInterceptor;
import cn.amaake.magicplugin.satoken.Interceptor.CustomRequestInterceptor;
import cn.amaake.magicplugin.satoken.config.MagicApiSaTokenConfig;
import cn.amaake.magicplugin.satoken.config.MagicSaOauth2Config;
import cn.amaake.magicplugin.satoken.filter.TokenResponseHeaderFilter;
import cn.amaake.magicplugin.satoken.interfaces.SatokenAbstractClientService;
import cn.amaake.magicplugin.satoken.interfaces.StpInterfaceImpl;
import cn.amaake.magicplugin.satoken.template.SaOAuth2TemplateImpl;
import cn.amaake.magicplugin.satoken.web.SaOAuth2ServerController;
import cn.amaake.magicplugin.satoken.web.SatokenUtilController;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.ssssssss.magicapi.core.config.MagicAPIProperties;
import org.ssssssss.magicapi.core.config.MagicConfiguration;
import org.ssssssss.magicapi.core.config.MagicPluginConfiguration;
import org.ssssssss.magicapi.core.model.Plugin;
import org.ssssssss.magicapi.core.web.MagicControllerRegister;
import org.ssssssss.magicapi.utils.Mapping;

@Configuration
@EnableConfigurationProperties(MagicApiSaTokenConfig.class)
public class MagicAPISaTokenConfiguration implements MagicPluginConfiguration {

    private final MagicAPIProperties properties;
    private final ApplicationContext applicationContext;
    private final MagicApiSaTokenConfig magicApiSaTokenConfig;


    public MagicAPISaTokenConfiguration(MagicAPIProperties properties, MagicApiSaTokenConfig magicApiSaTokenConfig, ApplicationContext applicationContext){
        this.properties = properties;
        this.magicApiSaTokenConfig = magicApiSaTokenConfig;
        this.applicationContext = applicationContext;
    }

    @Bean
    public TokenResponseHeaderFilter tokenResponseHeaderFilter(SaTokenConfig saTokenConfig){
        return new TokenResponseHeaderFilter(magicApiSaTokenConfig,saTokenConfig);
    }

    @Bean
    public CustomAuthorizationInterceptor customAuthorizationInterceptor(ApplicationContext applicationContext){
        return new CustomAuthorizationInterceptor(magicApiSaTokenConfig,applicationContext);
    }

    @Bean
    public CustomRequestInterceptor customRequestInterceptor(ApplicationContext applicationContext,SaTokenConfig saTokenConfig){
        return new CustomRequestInterceptor(magicApiSaTokenConfig,applicationContext,saTokenConfig);
    }

    @Bean
    public StpInterfaceImpl stpInterface(ApplicationContext applicationContext){
        return new StpInterfaceImpl(applicationContext);
    }



    @Bean
    @ConditionalOnProperty(prefix = "magic-api.satoken",name = "sa-token-mode",havingValue = "oauth2server")
    public SaOAuth2TemplateImpl saOAuth2TemplateImpl(ApplicationContext applicationContext){
        return new SaOAuth2TemplateImpl(applicationContext);
    }

    @Bean
    @ConditionalOnProperty(prefix = "magic-api.satoken",name = "sa-token-mode",havingValue = "oauth2server")
    public SaOAuth2ServerController saOAuth2ServerController(){
        return new SaOAuth2ServerController();
    }

    @Bean
    @ConditionalOnProperty(prefix = "magic-api.satoken",name = "sa-token-mode",havingValue = "oauth2client")
    public SatokenAbstractClientService satokenAbstractClientService(){
        return new SatokenAbstractClientService(magicApiSaTokenConfig);
    }


    /**
     *   注册Controller，如果有的话
     */
    @Override
    public MagicControllerRegister controllerRegister() {

        return new MagicControllerRegister() {
            @Override
            public void register(Mapping mapping, MagicConfiguration magicConfiguration) {
                mapping.registerController(new SatokenUtilController(magicApiSaTokenConfig,magicConfiguration,applicationContext));
//                if(magicApiSaTokenConfig.getSatokenmode()==SaTokenMode.OAUTH2SERVER){
//                    mapping.registerController(new SaOAuth2ServerController(magicConfiguration));
//                }
            }
        };
    }

    @Bean
    @ConditionalOnProperty(prefix = "magic-api.satoken",name = "sa-token-mode",havingValue = "oauth2server")
    public MagicSaOauth2Config magicSaOauth2Config(ApplicationContext applicationContext,SaOAuth2Config saOAuth2Config) {
        return new MagicSaOauth2Config(applicationContext,saOAuth2Config);
    }

    @Override
    public Plugin plugin() {
        return new Plugin("SaToken鉴权插件");
    }
}
